/*
* All code licensed under the GPLv2 and CDDL.
* Please visit http://www.gnu.org/licenses/gpl-2.0.html
* and http://www.sun.com/cddl/cddl.html for the full versions of
* these licenses.
 */

/*
 * TwitterappView.java
 */

package twitterapp;

import org.jdesktop.application.Action;
import org.jdesktop.application.ResourceMap;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import org.jdesktop.application.TaskMonitor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * The application's main frame.
 */
public class TwitterappView extends FrameView {
    //The program will pass the output of retrieving data from the server to
    //this variable, which will be used in this class's methods.
    public String Output = "";

    public TwitterappView(SingleFrameApplication app) {
        super(app);

        initComponents();

        // status bar initialization - message timeout,
        //idle icon and busy animation, etc
        ResourceMap resourceMap = getResourceMap();
        int messageTimeout = resourceMap.getInteger("StatusBar.messageTimeout");
        messageTimer = new Timer(messageTimeout, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                statusMessageLabel.setText("");
            }
        });
        messageTimer.setRepeats(false);
        int busyAnimationRate = resourceMap.getInteger
                ("StatusBar.busyAnimationRate");
        for (int i = 0; i < busyIcons.length; i++) {
            busyIcons[i] = resourceMap.getIcon
                    ("StatusBar.busyIcons[" + i + "]");
        }
        busyIconTimer = new Timer(busyAnimationRate, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                busyIconIndex = (busyIconIndex + 1) % busyIcons.length;
                statusAnimationLabel.setIcon(busyIcons[busyIconIndex]);
            }
        });
        idleIcon = resourceMap.getIcon("StatusBar.idleIcon");
        statusAnimationLabel.setIcon(idleIcon);

        // connecting action tasks to status bar via TaskMonitor
        TaskMonitor taskMonitor = new TaskMonitor(getApplication().getContext());
        taskMonitor.addPropertyChangeListener(new java.beans.
                PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                String propertyName = evt.getPropertyName();
                if ("started".equals(propertyName)) {
                    if (!busyIconTimer.isRunning()) {
                        statusAnimationLabel.setIcon(busyIcons[0]);
                        busyIconIndex = 0;
                        busyIconTimer.start();
                    }
                } else if ("done".equals(propertyName)) {
                    busyIconTimer.stop();
                    statusAnimationLabel.setIcon(idleIcon);
                } else if ("message".equals(propertyName)) {
                    String text = (String)(evt.getNewValue());
                    statusMessageLabel.setText((text == null) ? "" : text);
                    messageTimer.restart();
                } else if ("progress".equals(propertyName)) {
                    //int value = (Integer)(evt.getNewValue());
                }
            }
        });
        //Set some initial properties...
        FriendsTimelineRadio.setSelected(true);
        PasswordField.setEchoChar('*');
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = TwitterappApp.getApplication().getMainFrame();
            aboutBox = new TwitterappAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        TwitterappApp.getApplication().show(aboutBox);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        UsernameLabel = new javax.swing.JLabel();
        PasswordLabel = new javax.swing.JLabel();
        UsernameField = new javax.swing.JTextField();
        ConnectButton = new javax.swing.JButton();
        RememberPassTick = new javax.swing.JCheckBox();
        PasswordField = new javax.swing.JPasswordField();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        PostsMenu = new javax.swing.JMenu();
        NewPostMenuItem = new javax.swing.JMenuItem();
        DirectMessageMenuItem = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        Refresh = new javax.swing.JMenuItem();
        ContactsMenu = new javax.swing.JMenu();
        NewContactMenuItem = new javax.swing.JMenuItem();
        ViewFollowersMenuItem = new javax.swing.JMenuItem();
        AccountsMenu = new javax.swing.JMenu();
        ViewAccountsMenuItem = new javax.swing.JMenuItem();
        NewAccountMenuItem = new javax.swing.JMenuItem();
        DisconnectAll = new javax.swing.JMenuItem();
        ToolsMenu = new javax.swing.JMenu();
        PreferencesMenuItem = new javax.swing.JMenuItem();
        ViewMenu = new javax.swing.JMenu();
        PublicTimelineRadio = new javax.swing.JRadioButtonMenuItem();
        FriendsTimelineRadio = new javax.swing.JRadioButtonMenuItem();
        MyTimelineRadio = new javax.swing.JRadioButtonMenuItem();
        DirectMessagesRadio = new javax.swing.JRadioButtonMenuItem();
        DirectRepliesRadio = new javax.swing.JRadioButtonMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();
        HelpMenuItem = new javax.swing.JMenuItem();
        statusPanel = new javax.swing.JPanel();
        javax.swing.JSeparator statusPanelSeparator = new javax.swing.JSeparator();
        statusMessageLabel = new javax.swing.JLabel();
        statusAnimationLabel = new javax.swing.JLabel();
        ViewRadioGroup = new javax.swing.ButtonGroup();

        mainPanel.setName("mainPanel"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(twitterapp.TwitterappApp.class).getContext().getResourceMap(TwitterappView.class);
        UsernameLabel.setText(resourceMap.getString("UsernameLabel.text")); // NOI18N
        UsernameLabel.setName("UsernameLabel"); // NOI18N

        PasswordLabel.setText(resourceMap.getString("PasswordLabel.text")); // NOI18N
        PasswordLabel.setName("PasswordLabel"); // NOI18N

        UsernameField.setText(resourceMap.getString("UsernameField.text")); // NOI18N
        UsernameField.setName("UsernameField"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(twitterapp.TwitterappApp.class).getContext().getActionMap(TwitterappView.class, this);
        ConnectButton.setAction(actionMap.get("ConnectInvoke")); // NOI18N
        ConnectButton.setText(resourceMap.getString("ConnectButton.text")); // NOI18N
        ConnectButton.setName("ConnectButton"); // NOI18N

        RememberPassTick.setText(resourceMap.getString("RememberPassTick.text")); // NOI18N
        RememberPassTick.setName("RememberPassTick"); // NOI18N

        PasswordField.setText(resourceMap.getString("PasswordField.text")); // NOI18N
        PasswordField.setName("PasswordField"); // NOI18N

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(ConnectButton)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(UsernameLabel)
                            .addComponent(PasswordLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(PasswordField)
                            .addComponent(UsernameField, javax.swing.GroupLayout.DEFAULT_SIZE, 117, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                        .addComponent(RememberPassTick)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addContainerGap(153, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(UsernameLabel)
                    .addComponent(UsernameField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(PasswordLabel)
                    .addComponent(PasswordField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(12, 12, 12)
                .addComponent(RememberPassTick)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(ConnectButton)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        PostsMenu.setText(resourceMap.getString("PostsMenu.text")); // NOI18N
        PostsMenu.setName("PostsMenu"); // NOI18N

        NewPostMenuItem.setText(resourceMap.getString("NewPostMenuItem.text")); // NOI18N
        NewPostMenuItem.setName("NewPostMenuItem"); // NOI18N
        PostsMenu.add(NewPostMenuItem);

        DirectMessageMenuItem.setText(resourceMap.getString("DirectMessageMenuItem.text")); // NOI18N
        DirectMessageMenuItem.setName("DirectMessageMenuItem"); // NOI18N
        PostsMenu.add(DirectMessageMenuItem);

        jSeparator1.setName("jSeparator1"); // NOI18N
        PostsMenu.add(jSeparator1);

        Refresh.setText(resourceMap.getString("Refresh.text")); // NOI18N
        Refresh.setName("Refresh"); // NOI18N
        PostsMenu.add(Refresh);

        menuBar.add(PostsMenu);

        ContactsMenu.setText(resourceMap.getString("ContactsMenu.text")); // NOI18N
        ContactsMenu.setName("ContactsMenu"); // NOI18N

        NewContactMenuItem.setText(resourceMap.getString("NewContactMenuItem.text")); // NOI18N
        NewContactMenuItem.setName("NewContactMenuItem"); // NOI18N
        ContactsMenu.add(NewContactMenuItem);

        ViewFollowersMenuItem.setText(resourceMap.getString("ViewFollowersMenuItem.text")); // NOI18N
        ViewFollowersMenuItem.setName("ViewFollowersMenuItem"); // NOI18N
        ContactsMenu.add(ViewFollowersMenuItem);

        menuBar.add(ContactsMenu);

        AccountsMenu.setText(resourceMap.getString("AccountsMenu.text")); // NOI18N
        AccountsMenu.setName("AccountsMenu"); // NOI18N

        ViewAccountsMenuItem.setText(resourceMap.getString("ViewAccountsMenuItem.text")); // NOI18N
        ViewAccountsMenuItem.setName("ViewAccountsMenuItem"); // NOI18N
        AccountsMenu.add(ViewAccountsMenuItem);

        NewAccountMenuItem.setText(resourceMap.getString("NewAccountMenuItem.text")); // NOI18N
        NewAccountMenuItem.setName("NewAccountMenuItem"); // NOI18N
        AccountsMenu.add(NewAccountMenuItem);

        DisconnectAll.setText(resourceMap.getString("DisconnectAll.text")); // NOI18N
        DisconnectAll.setName("DisconnectAll"); // NOI18N
        AccountsMenu.add(DisconnectAll);

        menuBar.add(AccountsMenu);

        ToolsMenu.setText(resourceMap.getString("ToolsMenu.text")); // NOI18N
        ToolsMenu.setName("ToolsMenu"); // NOI18N

        PreferencesMenuItem.setText(resourceMap.getString("PreferencesMenuItem.text")); // NOI18N
        PreferencesMenuItem.setName("PreferencesMenuItem"); // NOI18N
        ToolsMenu.add(PreferencesMenuItem);

        menuBar.add(ToolsMenu);

        ViewMenu.setText(resourceMap.getString("ViewMenu.text")); // NOI18N
        ViewMenu.setName("ViewMenu"); // NOI18N

        ViewRadioGroup.add(PublicTimelineRadio);
        PublicTimelineRadio.setText(resourceMap.getString("PublicTimelineRadio.text")); // NOI18N
        PublicTimelineRadio.setName("PublicTimelineRadio"); // NOI18N
        ViewMenu.add(PublicTimelineRadio);

        ViewRadioGroup.add(FriendsTimelineRadio);
        FriendsTimelineRadio.setText(resourceMap.getString("FriendsTimelineRadio.text")); // NOI18N
        FriendsTimelineRadio.setName("FriendsTimelineRadio"); // NOI18N
        ViewMenu.add(FriendsTimelineRadio);

        ViewRadioGroup.add(MyTimelineRadio);
        MyTimelineRadio.setText(resourceMap.getString("MyTimelineRadio.text")); // NOI18N
        MyTimelineRadio.setName("MyTimelineRadio"); // NOI18N
        ViewMenu.add(MyTimelineRadio);

        ViewRadioGroup.add(DirectMessagesRadio);
        DirectMessagesRadio.setText(resourceMap.getString("DirectMessagesRadio.text")); // NOI18N
        DirectMessagesRadio.setName("DirectMessagesRadio"); // NOI18N
        ViewMenu.add(DirectMessagesRadio);

        ViewRadioGroup.add(DirectRepliesRadio);
        DirectRepliesRadio.setSelected(true);
        DirectRepliesRadio.setText(resourceMap.getString("DirectRepliesRadio.text")); // NOI18N
        DirectRepliesRadio.setName("DirectRepliesRadio"); // NOI18N
        ViewMenu.add(DirectRepliesRadio);

        menuBar.add(ViewMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        HelpMenuItem.setText(resourceMap.getString("HelpMenuItem.text")); // NOI18N
        HelpMenuItem.setName("HelpMenuItem"); // NOI18N
        helpMenu.add(HelpMenuItem);

        menuBar.add(helpMenu);

        statusPanel.setName("statusPanel"); // NOI18N

        statusPanelSeparator.setName("statusPanelSeparator"); // NOI18N

        statusMessageLabel.setName("statusMessageLabel"); // NOI18N

        statusAnimationLabel.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        statusAnimationLabel.setName("statusAnimationLabel"); // NOI18N

        javax.swing.GroupLayout statusPanelLayout = new javax.swing.GroupLayout(statusPanel);
        statusPanel.setLayout(statusPanelLayout);
        statusPanelLayout.setHorizontalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(statusPanelSeparator, javax.swing.GroupLayout.DEFAULT_SIZE, 379, Short.MAX_VALUE)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(statusMessageLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 355, Short.MAX_VALUE)
                .addComponent(statusAnimationLabel)
                .addContainerGap())
        );
        statusPanelLayout.setVerticalGroup(
            statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(statusPanelLayout.createSequentialGroup()
                .addComponent(statusPanelSeparator, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(statusPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(statusMessageLabel)
                    .addComponent(statusAnimationLabel))
                .addGap(3, 3, 3))
        );

        setComponent(mainPanel);
        setMenuBar(menuBar);
        setStatusBar(statusPanel);
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void ConnectInvoke() {
        //Get the username and password strings to ensure they're not blank.
        String Username = UsernameField.getText();
        String Password = PasswordField.getPassword().toString();
        //A variable for checking whether to remember the password or not.
        int rememberpass = 0;
        if (Username.length() == 0)
        {
            nouser();
        }
        else if (Password.length() == 0)
        {
            nopass();
        }
        else
        {
            if (RememberPassTick.isSelected() == false)
            {
                Output = TwitterappApp.connect(UsernameField.getText(),
                        PasswordField.getText());
            }
            else if (RememberPassTick.isSelected() == true)
            {
                rememberpass = JOptionPane.showConfirmDialog(null,
                        "You have chosen to remember the password." +
                        "\n" +
                        "Are you sure you want to do this?" +
                        "\nThis is not secure.");
                if (rememberpass == 0)
                {
                    JOptionPane.showMessageDialog(null, "Not implemented yet." +
                            "Won't save password.\nContinuing anyway...");
                    Output = TwitterappApp.connect(UsernameField.getText(),
                            PasswordField.getText());
                }
                else if (rememberpass == 1)
                {
                    JOptionPane.showMessageDialog(null, "Not implemented yet.");
                    Output = TwitterappApp.connect(UsernameField.getText(),
                            PasswordField.getText());
                }
            }
            TwitterappApp.HandleData(Output, "account/verify_credentials");
        }
    }

    private static void nopass()
    {
        JOptionPane.showMessageDialog(null, "No password entered;" +
                "\nYou cannot login without a username");
    }

    private static void nouser()
    {
        JOptionPane.showMessageDialog(null, "No username entered;" +
                "\nYou cannot login without a username");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenu AccountsMenu;
    private javax.swing.JButton ConnectButton;
    private javax.swing.JMenu ContactsMenu;
    private javax.swing.JMenuItem DirectMessageMenuItem;
    private javax.swing.JRadioButtonMenuItem DirectMessagesRadio;
    private javax.swing.JRadioButtonMenuItem DirectRepliesRadio;
    private javax.swing.JMenuItem DisconnectAll;
    private javax.swing.JRadioButtonMenuItem FriendsTimelineRadio;
    private javax.swing.JMenuItem HelpMenuItem;
    private javax.swing.JRadioButtonMenuItem MyTimelineRadio;
    private javax.swing.JMenuItem NewAccountMenuItem;
    private javax.swing.JMenuItem NewContactMenuItem;
    private javax.swing.JMenuItem NewPostMenuItem;
    private javax.swing.JPasswordField PasswordField;
    private javax.swing.JLabel PasswordLabel;
    private javax.swing.JMenu PostsMenu;
    private javax.swing.JMenuItem PreferencesMenuItem;
    private javax.swing.JRadioButtonMenuItem PublicTimelineRadio;
    private javax.swing.JMenuItem Refresh;
    private javax.swing.JCheckBox RememberPassTick;
    private javax.swing.JMenu ToolsMenu;
    private javax.swing.JTextField UsernameField;
    private javax.swing.JLabel UsernameLabel;
    private javax.swing.JMenuItem ViewAccountsMenuItem;
    private javax.swing.JMenuItem ViewFollowersMenuItem;
    private javax.swing.JMenu ViewMenu;
    private javax.swing.ButtonGroup ViewRadioGroup;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JLabel statusAnimationLabel;
    private javax.swing.JLabel statusMessageLabel;
    private javax.swing.JPanel statusPanel;
    // End of variables declaration//GEN-END:variables

    private final Timer messageTimer;
    private final Timer busyIconTimer;
    private final Icon idleIcon;
    private final Icon[] busyIcons = new Icon[15];
    private int busyIconIndex = 0;

    private JDialog aboutBox;
}
